package com.atguigu.commo.result;

import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.Data;

@Data
@Schema(description = "统一响应类")
public class Result<T>{
    @Schema(description = "业务状态码： 0表示cg")
    private Integer code;
    private String message;
    private Boolean ok;
    private T data;
    private Result(){}

    public static  <T> Result<T> build(Integer code,String message,Boolean ok,T data){
        Result<T> result = new Result<>();
        result.code=code;
        result.message=message;
        result.data=data;
        result.ok= ok;
        return  result;
    }
    public static <T> Result<T> build(ResultCodeEnum codeEnum,Boolean ok,T data){
        return build(codeEnum.getCode(), codeEnum.getMessage(), ok, data);
    }
    public static <T> Result<T> ok(T data){
        return build(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), true, data);
    }
    public static <T> Result<T> ok(){
        return build(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), true, null);
    }
    public static <T> Result<T> fail(Integer code,String message){
        return  build(code, message, false, null);
    }
    public static <T> Result<T> fail(){
        return  fail(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage());
    }
    public static <T> Result<T> fail(ResultCodeEnum codeEnum){
        return build(codeEnum.getCode(), codeEnum.getMessage(), false, null);
    }
    public static <T> Result<T> build(boolean b){
        return  b?Result.ok():Result.fail();
    }
    public Result<T> code(Integer code){
        this.code = code;
        return this;
    }
    public Result<T> message(String message){
        this.message = message;
        return this;
    }
    public Result<T> ok(Boolean ok){
        this.ok = ok;
        return this;
    }
    public Result<T> data(T data){
        this.data = data;
        return this;
    }

}
